package vn.gamesv.stores;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.gamesv.stores.detail.GameSVRomanticDetail;
import vn.gamesv.stores.jsonParsing.JSONParser;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class GameSVRomantic extends ListActivity {

	private ListView lvStory;
	TextView titleHeader;
	private static String URL_LIST_ROMAN = ""; //set url list romantic at here

	private static final String TAG_LIST_ROMAN = "list_roman";
	private static final String TAG_ID = "id";
	private static final String TAG_TITLE = "title";
	private static final String TAG_VIEW_NUMBER = "view_number";
	
	private String mId = null;

	JSONArray roman = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_item_story);
		titleHeader = (TextView) findViewById(R.id.title_header);
		Typeface tf = Typeface.createFromAsset(getAssets(),
				"fonts/utm_cokies.ttf");
		titleHeader.setText("Truyện tình yêu");
		titleHeader.setTypeface(tf);

		// Hashmap for ListView
		ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

		// Creating JSON Parser instance
		JSONParser jParser = new JSONParser();

		// getting JSON string from URL
		JSONObject json = jParser.getJSONFromUrl(URL_LIST_ROMAN);

		try {
			// Getting Array of Contacts
			roman = json.getJSONArray(TAG_LIST_ROMAN);

			// looping through All Contacts
			for (int i = 0; i < roman.length(); i++) {
				JSONObject c = roman.getJSONObject(i);

				// Storing each json item in variable
				mId = c.getString(TAG_ID);
				String title = c.getString(TAG_TITLE);
				String viewNumber = c.getString(TAG_VIEW_NUMBER);

				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();

				// adding each child node to HashMap key => value
				map.put(TAG_ID, mId);
				map.put(TAG_TITLE, title);
				map.put(TAG_VIEW_NUMBER, viewNumber);

				// adding HashList to ArrayList
				contactList.add(map);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		ListAdapter adapter = new SimpleAdapter(this, contactList,
				R.layout.list_item_detail, new String[] { TAG_TITLE,
						TAG_VIEW_NUMBER }, new int[] { R.id.title_list_item,
						R.id.view_number });
		 lvStory = (ListView) findViewById(R.id.listItemStories);
		 
		 setListAdapter(adapter);

			// selecting single ListView item
			ListView lv = getListView();

			// Launching new screen on Selecting Single ListItem
			lv.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// getting values from selected ListItem
					String title = ((TextView) view.findViewById(R.id.title_list_item)).getText().toString();
					// Starting new intent
					Intent in = new Intent(getApplicationContext(), GameSVRomanticDetail.class);
					in.putExtra(TAG_ID, mId);
					in.putExtra(TAG_TITLE, title);
					startActivity(in);

				}
			});
	}
}
